<template>
  <view class="order-info">
    <view class="nav-bar-wrap">
      <uni-nav-bar :border="false" :fixed="true" status-bar left-icon="left" :title="title|i18n" @clickLeft="back" />
    </view>
    <view class="order-info-status">
      <view class="order-price">￥{{price}}</view>
      <view class="order-status">{{statusName}}</view>
    </view>
    <view class="order-info-list">
      <view class="order-info-list-item" v-for="info in infos" :key="info.label">
        <view class="item-label">{{info.label|i18n}}</view>
        <view class="item-value">{{info.value}}</view>
      </view>
    </view>
  </view>
</template>

<script>
import {mapGetters} from 'vuex'
import {
  fetchIllegalOrderInfo,
  fetchElectricityOrderInfo,
  fetchTelephoneChargesOrderInfo,
  fetchTrafficOrderInfo
} from "../../../api/dailyLife";
import {
  ILLEGAL_ORDER_TYPE,
  ELECTRICITY_ORDER_TYPE,
  TELEPHONE_CHARGES_ORDER_TYPE,
  TRAFFIC_ORDER_TYPE
} from "../../../consts";

const API_BY_ORDER_TYPE = {
  [ILLEGAL_ORDER_TYPE]: fetchIllegalOrderInfo,
  [ELECTRICITY_ORDER_TYPE]: fetchElectricityOrderInfo,
  [TELEPHONE_CHARGES_ORDER_TYPE]: fetchTelephoneChargesOrderInfo,
  [TRAFFIC_ORDER_TYPE]: fetchTrafficOrderInfo
}
const TITLE_BY_ORDER_TYPE = {
  [ILLEGAL_ORDER_TYPE]: '违章缴费详情',
  [ELECTRICITY_ORDER_TYPE]: '电费充值详情',
  [TELEPHONE_CHARGES_ORDER_TYPE]: '话费充值详情',
  [TRAFFIC_ORDER_TYPE]: '流量充值详情'
}

export default {
  name: "index",
  data () {
    return {
      orderType: -1,
      price: '',
      statusName: '',
      infos: [
        {
          label: '付款方式',
          value: '--'
        },
        {
          label: '违章单号',
          value: '--'
        },
        {
          label: '面值',
          value: '--'
        },
        {
          label: '交易时间',
          value: '--'
        },
        {
          label: '交易渠道',
          value: '--'
        },
        {
          label: '订单号',
          value: '--'
        },
        {
          label: '商家订单号',
          value: '--'
        },
      ]
    }
  },
  computed: {
    title () {
      return TITLE_BY_ORDER_TYPE[this.orderType] || '订单详情'
    },
    ...mapGetters(['isLogin', 'uid'])
  },
  onLoad (options) {
    const {orderId, orderType} = options
    this.orderId = orderId
    this.orderType = +orderType
    this.getOrderInfo()
  },
  methods: {
    getOrderInfo () {
      let data = {
        orderId: this.orderId,
        userId: this.uid
      }
      uni.showLoading()
      API_BY_ORDER_TYPE[this.orderType](data).then((res) => {
        const {code, data, msg} = res
        if (code === 1) {
          const {channel, createTime, fineNum, money, cardNum, number, orderNo, orderStatus, payType, tradeNo} = data
          this.price = money
          this.statusName = orderStatus
          this.infos[0].value = payType
          if (this.orderType === ILLEGAL_ORDER_TYPE) {
            this.infos[1].label = '违章单号'
            this.infos[1].value = fineNum
          } else if (this.orderType === ELECTRICITY_ORDER_TYPE) {
            this.infos[1].label = '电卡号'
            this.infos[1].value = cardNum
          } else if (this.orderType === TELEPHONE_CHARGES_ORDER_TYPE) {
            this.infos[1].label = '充值号码'
            this.infos[1].value = number
          } else if (this.orderType === TRAFFIC_ORDER_TYPE) {
            this.infos[1].label = '充值号码'
            this.infos[1].value = number
          }
          this.infos[2].value = money + this.$i18n['元']
          this.infos[3].value = createTime
          this.infos[4].value = channel
          this.infos[5].value = orderNo
          this.infos[6].value = tradeNo
        } else {
          console.error(msg)
        }
      }).finally(() => {
        uni.hideLoading()
      })
    },
    back () {
      uni.navigateBack()
    }
  }
}
</script>

<style lang="less" scoped>
.order-info {
  height: 100vh;
  background: #ffffff;
}
.order-info-status {
  padding: 80upx 0 50upx;
  text-align: center;
  .order-price {
    font-size: 44upx;
    font-weight: 500;
    color: #333333;
    line-height: 72upx;
  }
  .order-status {
    margin-top: 12upx;
    font-size: 28upx;
    font-weight: 400;
    color: #999999;
    line-height: 36upx;
  }
}
.order-info-list {
  padding: 0 44upx;
  .order-info-list-item {
    display: flex;
    border-top: 1px solid #F6F6F6;
    padding: 28upx 0;
    justify-content: space-between;
    direction: rtl;
    .item-label {
      flex: 0 0 220upx;
      font-size: 28upx;
      font-weight: 400;
      color: #666666;
      line-height: 36upx;
    }
    .item-value {
      white-space: normal;
      word-break: break-all;
      word-wrap: break-word;
      font-size: 28upx;
      font-weight: 400;
      color: #333333;
      line-height: 36upx;
    }
  }
  .order-info-list-item:last-child {
    border-bottom: 1px solid #F6F6F6;
  }
}
</style>